<?php
namespace admin\controller;
use \core\Controller;
header("content-type:text/html;charset=utf-8");
class ArticleController extends Controller{
        //列表页
       public function showList()
       {
       //接受表单数据
           $s_title=isset($_POST['s_title'])?addslashes(trim($_POST['s_title'])):null;
           $s_cat_id=isset($_POST['s_cat_id'])?addslashes(trim($_POST['s_cat_id'])):Null;
           $s_status=isset($_POST['s_status'])?addslashes(trim($_POST['s_status'])):Null;
           //拼接查询条件
           $condition='';

           if($s_title!=null){
               $condition.= 'a.title like "%'.$s_title.'%"';
           }

           if(!empty($s_cat_id)){
               if(empty($condition)) {
                   $condition .= 'a.cat_id='.$s_cat_id;
               }else{
                   $condition .= 'and a.cat_id='.$s_cat_id;
               }
           }
           if(!empty($s_status)){
               if(empty($condition)) {
                   $condition .= 'a.status='.$s_status;
               }else{
                   $condition .= 'and a.status='.$s_status;
               }
           }
           //做博文列表
           $model=SF('\\model\\ArticleModel');


       //分页功能
           $nowPage=empty($_GET['page'])?1:$_GET['page'];//当前页
           $num_per_page=2;//每页行数
           $total_rows=$model->all_rows($condition);//总行数

           $total_page=intval(ceil($total_rows['num']/$num_per_page));//总页数
           $x=($nowPage-1)*$num_per_page;//设置初始值
           //查询数据
//           $condition1=empty($condition)? $condition.'1 limit '.$x.','.$num_per_page : $condition.' limit '.$x.','.$num_per_page;
           $condition=empty($condition)? 1:$condition;
           $condition1=$condition ." limit $x,$num_per_page";

           $article=$model->joinCat($condition1);
           $url=C('url.main').'?p=admin&m=Article&a=showList&page';
           $strPage=pageHtml($nowPage,$total_page,$url);

           //做分类
           $catModel = SF('\\model\\CategoryModel');
           $catRows=$catModel->getRows('id,name,parent_id','bl_category',1);
           //调用无限极变量
           $tree=array();
          $catModel->recursiveCat($tree,$catRows);

           $this->assign('tree',$tree);
//           $this->assign('article',$rows);
           $this->assign('s_title',$s_title);
           $this->assign('s_cat_id',$s_cat_id);
           $this->assign('s_status',$s_status);

           $this->assign('article',$article);
           $this->assign('strPage',$strPage);



           $this->display('\Article\articleIndex.html');

       }



       //新增页
        public function showAd(){
            //调用category MODEL 来实现
            $catModel=SF('\\model\\CategoryModel');
            $catRows=$catModel->getRows('id,name,parent_id','bl_category','1');

            $tree=array();
            $catModel->recursiveCat($tree,$catRows);

            //分配
            $this->assign('tree',$tree);

            $this->display('\Article\articleAdd.html');

        }
        //新增部分
    public function Ad()
    {

        $title = trim($_POST['title']);
        $cat_id = trim($_POST['category']);
        $content = htmlspecialchars($_POST['content']);
        $intro = trim($_POST['intro']);
        $status = trim($_POST['status']);
        $pubtime = time();
        $ad_account =$_SESSION['admin']['account']; //当前发表文章的管理员账号
        $ad_id =$_SESSION['admin']['id'];


        $model= SF('\\model\\ArticleModel');
        $val = "null,'$cat_id','$title','$intro','$content','$status','$pubtime','$ad_account','$ad_id'";
     
        if ($model->ad('bl_article', $val)) {
            echo $this->jump('新增成功','p=admin&m=Article&a=showList',1);;
        }else{
            echo $this->jump('新增失败','p=admin&m=Article&a=showList',1);
        }

    }

        //修改页
    public function showUpd(){

        $catModel = SF('\\model\\CategoryModel');
        $catRows=$catModel->getRows('id,name,parent_id','bl_category','1');
        //调用无限极变量
        $tree=array();
        $catModel->recursiveCat($tree,$catRows);
        $this->assign('tree',$tree);


        $id=$_GET['id'];
        $artmodel=SF('\\model\\ArticleModel');
        $val="id,cat_id,title,intro,content,status,pubtime";
        $article=$artmodel->getRow($val,'bl_article','id='.$id);
//            var_dump($article['']);
        $pubtime=date('Y-M-D H:i',$article['pubtime']);
        $this->assign('article',$article);
        $this->assign('pubtime',$pubtime);

        $this->display('\\Article\\articleEdit.html');

    }
    //修改方法
            public function upd(){
                //接受数据
                $id=$_GET['id'];
                $cat_id=$_POST['cat_id'];
                $title=$_POST['title'];
                $pubtime=strtotime(trim($_POST['pubtime']));
                $intro=$_POST['intro'];
                $content=$_POST['content'];
                $status=$_POST['status'];

                $artmodel=SF('\\model\\ArticleModel');
                $val="id='$id',cat_id='$cat_id',intro='$intro',title='$title',content='$content',status='$status',pubtime='$pubtime'";
               if($artmodel->upd('bl_article',$val,'id='.$id)){
                   echo $this->jump('更新成功','p=admin&m=Article&a=showList',1);
               }else {
                   echo $this->jump('更新失败','p=admin&m=Article&a=showList',1);
               }

    }

    //删除功能
    public function del(){
                $id=$_GET['id'];
                $model=SF('\\model\\ArticleModel');

                if($model->del('bl_article','id='.$id)){
                    echo $this->jump('删除成功','p=admin&m=Article&a=showList',1);
                }else{
                    echo $this->jump('删除失败','p=admin&m=Article&a=showList',1);
                }


    }


}